package cn.jaye.fabric.application;

import cn.jaye.fabric.application.command.ListPeerByChannelCommand;
import cn.jaye.fabric.application.command.ListPeerCommand;
import cn.jaye.fabric.common.constant.Global;
import cn.jaye.fabric.domain.service.RestTemplateService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2018/5/24.
 */
@Service
public class PeerApplicationService {

    @Autowired
    private RestTemplateService restTemplateService;


    private static final String PEER_LIST = "/peer/list";
    private static final String PEER_LIST_CHANNELNAME = "/peer/list/channelName";

    public JSONObject list(ListPeerCommand listPeerCommand) {
        String url = Global.FABRRIC_GATEWAY_ADDRESS + PEER_LIST;
        JSONObject postData = new JSONObject();
        postData.put("orgId", listPeerCommand.getOrgId());
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return resultObject;
    }

    public JSONObject listByChannel(ListPeerByChannelCommand listPeerByChannelCommand) {
        String url = Global.FABRRIC_GATEWAY_ADDRESS + PEER_LIST_CHANNELNAME;
        JSONObject postData = new JSONObject();
        postData.put("channelName", listPeerByChannelCommand.getChannelName());
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return resultObject;
    }
}
